import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:tree_hole/LoginPage.dart';

class UpdatePsw extends StatelessWidget{

  String _oldPassword;
  String _newPassword;
  String _token;
  Response response;
  int code;

  UpdatePsw(this._token);

  Widget build(BuildContext context) {

    _update(){
      Dio().post("http://121.196.101.7:8080/treehole/user/changePassword",
          options:Options(headers: {"token":this._token},),queryParameters: {"oldPassword":this._oldPassword,"newPassword":this._newPassword})
          .then((value) {
        response=value;
        print(response.data);
        code=response.data["code"];
        if(code==0){
          Fluttertoast.showToast(
            msg: "修改成功",
            backgroundColor: Colors.amber[50],
            textColor: Colors.black,
            fontSize: 15,
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
          );
          Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context){return LoginPage();},), (route) => true);
        }else{
          Fluttertoast.showToast(
            msg: response.data["message"],
            backgroundColor: Colors.amber[50],
            textColor: Colors.black,
            fontSize: 15,
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
          );
        }
      });
    }


    return Scaffold(
      appBar: new AppBar(
        title: new Text('修改寄语',style:TextStyle(color: Colors.black)),
        backgroundColor: Colors.white70,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);},
        ),
      ),
      body: Container(
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
            color: Colors.amber[50]
        ),
        child: Center(
            child:Container(
              height: 200,
              child: Column(
                children: [
                  Expanded(
                      flex: 3,
                      child: Container(
                        width: 290,
                        child: TextField(
                          decoration: InputDecoration(
                            hintText: "请输入旧密码",
                            filled: true,
                            fillColor: Colors.transparent,
                            enabledBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                Radius.circular(15),
                              ),
                              borderSide: BorderSide(
                                color: Colors.black,
                                width: 1,
                              ),
                            ),
                          ),
                          obscureText: true,
                          onChanged: (value){
                            this._oldPassword=value;
                          },
                        ),
                      )
                  ),
                  Expanded(
                      flex: 1,
                      child: SizedBox()),
                  Expanded(
                      flex: 3,
                      child: Container(
                        width: 290,
                        child: TextField(
                          decoration: InputDecoration(
                            hintText: "请输入新密码",
                            filled: true,
                            fillColor: Colors.transparent,
                            enabledBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                Radius.circular(15),
                              ),
                              borderSide: BorderSide(
                                color: Colors.black,
                                width: 1,
                              ),
                            ),
                          ),
                          obscureText: true,
                          onChanged: (value){
                            this._newPassword=value;
                          },
                        ),
                      )
                  ),
                  Expanded(
                      flex: 1,
                      child: SizedBox()),
                  Expanded(
                      flex: 3,
                      child: Container(
                        width: 290,
                        child: FlatButton(
                          child: Text("修改密码",style: TextStyle(fontSize: 16,color: Colors.black),),
                          color: Colors.greenAccent[100],
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10)
                          ),
                          onPressed: (){
                            _update();
                          },
                        ),
                      )
                  )
                ],
              ),
            )
        ),
      ),
    );
  }
}